/*
 * 文 件 名 : HomeActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-4
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.chat;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.fire8.dolphin.service.JaberClientService;
import com.fire8.dolphin.ui.DolphinApplication;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;
import com.fire8.dolphin.web.RequestUtils;

/**
 * 加好友
 * 
 *
 * @author  lixg
 * @version [版本号,2012-8-4]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class AddFriendActivity extends Activity
{
    JaberClientService service;
    DolphinApplication app;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        service = JaberClientService.getInstance();
        app = (DolphinApplication)getApplication();
        LinearLayout layout = new LinearLayout(this); //线性布局方式 
        layout.setOrientation( LinearLayout.VERTICAL ); //控件对其方式为垂直排列 
        //layout.setBackgroundColor( 0xff00ffff );
        Intent intent = getIntent();
        intent.getStringExtra("from");
        setTitle("好友请求");
        final String from = intent.getStringExtra("from");
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        TextView tv = new TextView(this);
        tv.setTextSize(20);
        tv.setText(from+"请求你加为好友,是否同意?");
        layout.addView(tv,params);
        
        Button btnAgree = new Button(this);
        btnAgree.setText("同意");
        Button btnUnAgree = new Button(this);
        btnUnAgree.setText("拒绝");
        layout.addView(btnAgree,params);
        layout.addView(btnUnAgree,params);
        setContentView(layout);
        
        btnAgree.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //同意加为好友
                new SendMsgTask().execute(app.getUserId(),from);
            }
        });
        btnUnAgree.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                service.responseToAddFriend(from, false);
                AddFriendActivity.this.finish();
            }
        });
    }
    
    class SendMsgTask extends AsyncTask<String, Void, String> 
    {

        @Override
        protected String doInBackground(String... params)
        {
            try {
                service.responseToAddFriend(params[1], true);
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("userId",params[0]));
                param.add(new BasicNameValuePair("friendAccount",params[1]));
                String json = RequestUtils.getInstall().getHttpJson(DolphinApplication.URL+"user/addFriend.php", param);
                LogUtil.d(json);
                return "添加好友成功";
            } catch (Exception e) {
                LogUtil.e("发送消息异常了", e);
                return "添加好友失败";
            }
            
        }

        @Override
        protected void onPostExecute(String result)
        {
            super.onPostExecute(result);
            if(!StringUtil.isEmpty(result))
            {
                Toast.makeText(AddFriendActivity.this, result, Toast.LENGTH_LONG).show();
                AddFriendActivity.this.finish();
            }
            //发送广播通知页面刷新
            Intent intent2 = new Intent();
            intent2.setAction("com.fire8.dolphin.ui.FriendActivity");
            sendBroadcast(intent2);
        }
        
    }
}
